<template>
    <div class="login_container">
        <div class="login">
          <!-- 头像区域 -->
          <div class="avatar_box">
            <img src="../assets/logo.png" alt="">
          </div>
          <!-- 登录表单区域 -->
          <el-form ref="loginForm" :rules="loginRules" :model="loginFormData">
            <!-- 用户名 -->
            <el-form-item prop="username">
              <el-input v-model="loginFormData.username" prefix-icon="iconfont icon-user"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
              <el-input v-model="loginFormData.password" prefix-icon="iconfont icon-3702mima"></el-input>
            </el-form-item>
            <!-- 按钮 -->
            <el-form-item class="btn">
                <el-button type="primary" @click="submitForm">登录</el-button>
                <el-button type="info" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // var checkEmail = (rule, value, callback) => {}
      // 登录表单绑定的数据对象
      loginFormData: {
        username: 'admin',
        password: '123456'
      },
      // 登录表单验证规则
      loginRules: {
        // 用户名验证规则
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }],
        // 密码验证规则
        password: [{ required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 点击登录按钮验证表单并登录跳转到home页面
    submitForm () {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return false
        const { data: res } = await this.$http.post('login', this.loginFormData)
        console.log(res)
        // 判断status值确定是否登录
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // 保存token值 并跳转 home 页面
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    resetForm () {
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
}
.avatar_box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  border-radius: 50%;
  background-color: #eee;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #fff;
  }
}
.el-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
  .btn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
